package com.elong.android.specialhouse.utils;

import android.content.Context;
import android.os.Environment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.JSONConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.City;
import com.elong.android.specialhouse.entity.response.OptionConfig;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * @ClassName: FileUtils
 * @Description: 文件操作工具类
 * @author kaiqi.li
 * @date 2015-8-3 下午3:29:37
 */
public class FileUtils {

	public static String tempPicFilePath;

	/**
	 * 保存城市信息
	 * @param context
	 * @param jo
	 * @return boolean
	 */
	public static boolean saveCitiesData(Context context, JSONObject jo) {
		byte[] data = jo.toJSONString().getBytes() ;
		DataOutputStream dos = null ;


		return true ;
	}

	/**
	 * 读取城市信息
	 *
	 * @param context
	 * @return
	 */
	public static String getCitiesData(Context context) {
		return getCitiesData(context, null);
	}

	public static String getCitiesData(Context context,String fileName){
		byte[] data = null ;
		try {
			data = readParse(context, fileName == null ? "cities_data.txt" : fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String str = null ;
		try {
			str = new String(data,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str ;
	}
	
	/**
	 * 读取asset文件夹下的文件
	 */
	public static byte[] readParse(Context context , String urlPath) throws IOException{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024] ;
		int len = 0 ;
		InputStream is = context.getAssets().open(urlPath);
		while((len = is.read(data)) != -1){
			outStream.write(data, 0, len);
		}
		is.close();
		return outStream.toByteArray();
	}

	public static List<OptionConfig> getApartmentTypeList(Context context) {

		List<OptionConfig> datas = null ;
		try {
			InputStream in = context.getResources().openRawResource(R.raw.housetypelist);
			int length = in.available() ;
			byte[] buffer = new byte[length] ;
			in.read(buffer);
			JSONObject result = JSONObject.parseObject(new String(buffer));
			JSONArray arrayResult = result.getJSONArray(JSONConstants.ATTR_OPTIONCONFIG_LIST);
			if (null != arrayResult) {
				datas = JSONArray.parseArray(JSONArray.toJSONString(arrayResult), OptionConfig.class);
			}
			return datas;
		} catch (IOException e) {
			e.printStackTrace();
			return datas;
		}
	}
	public static String retrieveApartmentTypeById(Context context ,int typeId) {
		List<OptionConfig> datas = getApartmentTypeList(context) ;
		for (OptionConfig item : datas) {
			if (typeId == item.getOptionValue()) {
				return item.getOptionName() ;
			}
		}
		return "" ;
	}

	/**
	 * 设置城市列表的 A B C 分隔
	 * @param cities
	 * @return
	 */
	public static List<City> addCityListSeparator(List<City> cities){
		List<City> mCities = new LinkedList<City>(cities);

		for (int i = 0; i < mCities.size() - 1 ; i++) {
			char firstLetter = mCities.get(i).getJianPin().toUpperCase().charAt(0);
			char firstLetterNext = mCities.get(i+1).getJianPin().toUpperCase().charAt(0);
			if (firstLetter < firstLetterNext) {
				City separator = new City() ;
				separator.setCityName(firstLetterNext+"");
				separator.setSeparator(true);
				mCities.add(i+1, separator);
				i++ ;
			}
		}
		City firstSeparator = new City() ;
		firstSeparator.setCityName("A");
		firstSeparator.setSeparator(true);
		mCities.add(0, firstSeparator);
		return mCities ;
	}

	/**
	 * 初始化剪裁图片时生成的临时图片的存储目录E
	 */
	public static void initTempPicFilePath() {
		tempPicFilePath = Environment.getExternalStorageDirectory() + AppConstants.PARENT_FILE_PATH + "/Temp/";
		File dir = new File(tempPicFilePath);
		if (!dir.exists()) {
			dir.mkdirs() ;
		}
	}
}
